package nodes;

import compiler.Compiler;
import compiler.SymbolTable;

import descriptors.AbstractDescr;
import descriptors.ArrayDescr;
import descriptors.ConstDescr;
import descriptors.TypeDescr;

public class ArrayNode extends AbstractNode {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private AbstractNode indexExpression,type;
	
	public ArrayNode(AbstractNode indexExpression,AbstractNode type, int line, int column){
		super(line,column);
		this.indexExpression=indexExpression;
		this.type=type;
	}
	
	@Override
	public AbstractDescr compile(
			SymbolTable symbolTable, Compiler compiler) {
		ConstDescr indexDescr= (ConstDescr)indexExpression.compile(symbolTable,compiler);
		AbstractDescr typeDescr=null;
		if (type instanceof IdentNode){
			typeDescr=symbolTable.search(compiler.level(), ((IdentNode)type).ident());
			// TODO: Check without this
			//if(typeDescr == null){
			//	typeDescr=new TypeDescr(1,compiler.level(),((IdentNode) type).ident());
			//}
		} // Case of record or array
		else {
			typeDescr=type.compile(symbolTable, compiler);
		}
		return new ArrayDescr(indexDescr.value(),typeDescr.getSize()*indexDescr.value(),typeDescr);
	}

	@Override
	public void print() {
		trace("ArrayNode");
		indexExpression.print();
		type.print();
		unindent();
	}

}
